﻿@model BulkEditListModel
@using Telerik.Web.Mvc.UI
@{
    var gridPageSize = EngineContext.Current.Resolve<IT.CMS.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
    
    //page title
    ViewBag.Title = T("Admin.Catalog.BulkEdit").Text;
}
@using (Html.BeginForm())
{
    <div class="section-header">
        <div class="title">
            <img src="@Url.Content("~/Areas/Admin/content/images/ico-catalog.png")" alt="" />
            @T("Admin.Catalog.BulkEdit")
        </div>
        <div class="options">
        </div>
    </div>
    
    <table width="100%">
        <tr>
            <td class="adminTitle">
                @Html.CmsLabelFor(model => model.SearchProductName):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => Model.SearchProductName)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.CmsLabelFor(model => model.SearchCategoryId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchCategoryId", Model.AvailableCategories)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.CmsLabelFor(model => model.SearchManufacturerId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchManufacturerId", Model.AvailableManufacturers)
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" id="search-products" class="t-button" value="@T("Admin.Common.Search")" />
            </td>
        </tr>
    </table>
    <p>
    </p>
    <table class="adminContent">
        <tr>
            <td>
                @(Html.Telerik().Grid<BulkEditProductVariantModel>()
        .Name("productvariants-grid")
        .DataKeys(keys =>
        {
            keys.Add(pv => pv.Id);
        })
        .ToolBar(commands =>
        {
            commands.SubmitChanges();
        })
        .DataBinding(dataBinding =>
            dataBinding.Ajax()
                .Select("BulkEditSelect", "ProductVariant")
                .Update("BulkEditSave", "ProductVariant")
        )
        .Columns(columns =>
        {
            columns.Bound(pv => pv.Name).ReadOnly().Width(300);
            columns.Bound(pv => pv.Sku).Width(100);
            columns.Bound(pv => pv.Price).Width(100);
            columns.Bound(pv => pv.OldPrice).Width(100);
            columns.Bound(pv => pv.ManageInventoryMethod).ReadOnly().Width(150);
            columns.Bound(pv => pv.StockQuantity).Width(100);
            columns.Bound(pv => pv.Published).Width(100);
            columns.Command(commands => commands.Delete().Text(T("Admin.Common.Delete").Text)).Title(T("Admin.Common.Delete").Text).Width(50);
            columns.Bound(x => x.Id)
                .ReadOnly()
                .Width(50)
                .Centered()
                .Template(x => Html.ActionLink(T("Admin.Common.View").Text, "Edit", new { id = x.Id }))
                .ClientTemplate("<a href=\"Edit/<#= Id #>\">" + T("Admin.Common.View").Text + "</a>")
                .Title(T("Admin.Common.View").Text);
        })
        .ClientEvents(events => events.OnDataBinding("Grid_onDataBinding").OnError("Grid_onError").OnSubmitChanges("Grid_onSubmitChanges"))
        .Editable(editing => editing.Mode(GridEditMode.InCell))
        .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
)
<script type="text/javascript">


    $(document).ready(function () {
        $('#search-products').click(function () {
            var grid = $('#productvariants-grid').data('tGrid');
            grid.currentPage = 1; //new search. Set page size to 1
            grid.ajaxRequest();
            return false;
        });
        
        $("#@Html.FieldIdFor(model => model.SearchProductName)").keydown(function (event) {
            if (event.keyCode == 13) {
                $("#search-products").click();
                return false;
            }
        });
    });

    function Grid_onError(args) {
        if (args.textStatus == "modelstateerror" && args.modelState) {
            var message = "Errors:\n";
            $.each(args.modelState, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "\n";
                    });
                }
            });
            args.preventDefault();
            alert(message);
        }
    }
    function Grid_onDataBinding(e) {
        var loadData = true;
        var grid = $(this).data('tGrid');
        if (grid.hasChanges()) {
            if (!confirm('@T("Admin.Common.LoseUnsavedChanges")')) {
                e.preventDefault();
                loadData = false;
            }
        }
        if (loadData) {
            var searchModel = {
                SearchProductName: $('#@Html.FieldIdFor(model => model.SearchProductName)').val(),
                SearchCategoryId: $('#SearchCategoryId').val(),
                SearchManufacturerId: $('#SearchManufacturerId').val()
            };
            e.data = searchModel;
        }
    }
    function Grid_onSubmitChanges(e) {
        //Pass search parameters

        e.values["SearchProductName"] = $('#@Html.FieldIdFor(model => model.SearchProductName)').val();
        e.values["SearchCategoryId"] = $('#SearchCategoryId').val();
        e.values["SearchManufacturerId"] = $('#SearchManufacturerId').val();
    }
</script>
            </td>
        </tr>
    </table>
}